<template>
  <BasicDrawer v-bind="$attrs" @register="registerDrawer" width="650px" class="system-role-permission-drawer" destroyOnClose showFooter>
    <template #title>
      角色权限配置
      <a-dropdown>
        <Icon icon="ant-design:more-outlined" class="more-icon" />
        <template #overlay>
          <a-menu @click="treeMenuClick">
            <a-menu-item key="checkAll">选择全部</a-menu-item>
            <a-menu-item key="cancelCheck">取消选择</a-menu-item>
            <div class="line"></div>
            <a-menu-item key="openAll">展开全部</a-menu-item>
            <a-menu-item key="closeAll">折叠全部</a-menu-item>
            <div class="line"></div>
            <a-menu-item key="relation">层级关联</a-menu-item>
            <a-menu-item key="standAlone">层级独立</a-menu-item>
          </a-menu>
        </template>
      </a-dropdown>
    </template>
    <BasicTree
      ref="treeRef"
      checkable
      :treeData
      :checkedKeys
      :expandedKeys
      :selectedKeys
      :clickRowToExpand="false"
      checkStrictly
      title="所拥有的的权限"
      @check="onCheck"
      @select="onTreeNodeSelect"
    >
      <template #title="{ slotTitle, ruleFlag, menuSourceType }">
        {{ slotTitle }}
        <Icon v-if="ruleFlag" icon="ant-design:align-left-outlined" style="margin-left: 5px; color: red" />
        <Icon v-if="menuSourceType === 'btd'" icon="ion:settings-outline" style="margin-left: 5px; color: var(--j-global-primary-color)" />
      </template>
    </BasicTree>
    <!--右下角按钮-->
    <template #footer>
      <a-button @click="closeDrawer">取消</a-button>
      <a-button @click="handleSubmit(false)" type="primary" :loading ghost style="margin-right: 0.8rem">仅保存</a-button>
      <a-button @click="handleSubmit(true)" type="primary" :loading>保存并关闭</a-button>
    </template>
    <RoleDataRuleDrawer @register="registerDrawer1" />
  </BasicDrawer>
</template>
<script lang="ts" setup>
  import { ref, unref } from 'vue';
  import { BasicDrawer, useDrawer, useDrawerInner } from '/@/components/Drawer';
  import { BasicTree, TreeItem } from '/@/components/Tree';
  import RoleDataRuleDrawer from './RoleDataRuleDrawer.vue';
  import { queryTreeListForRole, queryRolePermission, saveRolePermission, queryBtdAuthIdByRoleId } from '../role.api';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { ROLE_AUTH_CONFIG_KEY } from '/@/enums/cacheEnum';
  //树的信息
  const treeData = ref<TreeItem[]>([]);
  //树的全部节点信息
  const allTreeKeys = ref([]);
  //树的选择节点信息
  const checkedKeys = ref<any>([]);
  const defaultCheckedKeys = ref([]);
  //树的选中的节点信息
  const selectedKeys = ref([]);
  const roleId = ref('');
  const roleCode = ref('');
  //树的实例
  const treeRef = ref(null);
  const loading = ref(false);
  const btdPermissionId = ref('');

  //展开折叠的key
  const expandedKeys = ref<any>([]);
  //父子节点选中状态是否关联 true不关联，false关联
  const checkStrictly = ref<boolean>(false);
  const [registerDrawer1, { openDrawer: openDataRuleDrawer }] = useDrawer();
  const [registerDrawer, { setDrawerProps, closeDrawer }] = useDrawerInner(async (data) => {
    reset();
    setDrawerProps({ confirmLoading: false, loading: true });

    // 使百特搭权限设置iframe能优先加载，避免勾选菜单并没有弹出抽屉时，没有数据
    openDataRuleDrawer(true, { selectedData: { menuSourceType: 'btd', rootClassName: 'init-role-data-rule-drawer' } });
    setTimeout(() => openDataRuleDrawer(false), 10);

    roleId.value = data.roleId;
    roleCode.value = formatRoleCode(data.roleCode);

    // 百特搭权限ID
    btdPermissionId.value = await queryBtdAuthIdByRoleId({ role_id: unref(roleCode) });

    //初始化数据
    const roleResult = await queryTreeListForRole();
    treeData.value = translateTitle(roleResult.treeList);
    allTreeKeys.value = roleResult.ids;
    const localData = localStorage.getItem(ROLE_AUTH_CONFIG_KEY);
    if (localData) {
      const obj = JSON.parse(localData);
      obj.level && treeMenuClick({ key: obj.level });
      obj.expand && treeMenuClick({ key: obj.expand });
    } else {
      expandedKeys.value = roleResult.ids;
    }
    //初始化角色菜单数据
    const permResult = await queryRolePermission({ roleId: unref(roleId) });
    checkedKeys.value = permResult;
    defaultCheckedKeys.value = permResult;
    setDrawerProps({ loading: false });
  });
  /**
   * 翻译菜单名称
   */
  function translateTitle(data) {
    if (data?.length) {
      data.forEach((item) => {
        if (item.slotTitle) {
          const { t } = useI18n();
          if (item.slotTitle.includes("t('") && t) {
            item.slotTitle = new Function('t', `return ${item.slotTitle}`)(t);
          }
        }
        if (item.children?.length) {
          translateTitle(item.children);
        }
      });
    }
    return data;
  }

  function formatRoleCode(roleCode) {
    if (roleCode === 'admin') {
      return 'sysadmin';
    } else if (roleCode === 'lowdeveloper') {
      return 'app-admin';
    } else {
      return roleCode;
    }
  }
  /**
   * 点击选中
   * 2024-04-26
   * liaozhiyang
   */
  function onCheck(o, e) {
    const selectedData = e.node;
    const checked = e.checked;
    // checkStrictly: true=>层级独立，false=>层级关联.
    if (checkStrictly.value) {
      checkedKeys.value = o.checked ? o.checked : o;
    } else {
      const keys = getNodeAllKey(e.node, 'children', 'key');
      if (e.checked) {
        // 反复操作下可能会有重复的keys，得用new Set去重下
        checkedKeys.value = [...new Set([...checkedKeys.value, ...keys])];
      } else {
        const result = removeMatchingItems(checkedKeys.value, keys);
        checkedKeys.value = result;
      }
    }

    // 选中菜单, 给百特搭iframe发送选中事件
    // 取消选中, 给百特搭iframe发送取消选中事件
    if (checked) {
      sendMessage('OnMenuChecked', selectedData);
    } else {
      sendMessage('OnMenuUnChecked', selectedData);
    }
  }

  function sendMessage(type, selectedData) {
    const btdPermissionIframe = document.getElementById('btd-permission-iframe') as HTMLIFrameElement;
    const message = {
      type,
      data: JSON.stringify({
        permissionId: unref(btdPermissionId),
        appId: selectedData.menuResourceAppId,
        formKey: selectedData.menuResourceId,
        menuId: selectedData.value,
        pageType: selectedData.menuResourceType,
      }),
    };
    if (btdPermissionIframe) btdPermissionIframe?.contentWindow?.postMessage(message, '*');
  }
  /**
   * 2024-04-26
   * liaozhiyang
   * 删除相匹配数组的项
   */
  function removeMatchingItems(arr1, arr2) {
    // 使用哈希表记录 arr2 中的元素
    const hashTable = {};
    for (const item of arr2) {
      hashTable[item] = true;
    }
    // 使用 filter 方法遍历第一个数组，过滤出不在哈希表中存在的项
    return arr1.filter((item) => !hashTable[item]);
  }
  /**
   * 2024-04-26
   * liaozhiyang
   * 获取当前节点及以下所有子孙级的key
   */
  function getNodeAllKey(node: any, children: any, key: string) {
    const result: any = [];
    result.push(node[key]);
    const recursion = (data) => {
      data.forEach((item: any) => {
        result.push(item[key]);
        if (item[children]?.length) {
          recursion(item[children]);
        }
      });
    };
    node[children]?.length && recursion(node[children]);
    return result;
  }

  /**
   * 选中节点，打开数据权限抽屉
   */
  const selectedData = ref({});
  async function onTreeNodeSelect(key, selectedInfo) {
    if (key && key.length > 0) {
      selectedKeys.value = key;
    }
    // selectedInfo.selectedNodes?.[0] || selectedData.value
    // 这种写法是为了处理, 同一个菜单点击两次的场景
    const selectedMenuData = selectedInfo.selectedNodes?.[0] || selectedData.value;
    selectedData.value = selectedMenuData;
    openDataRuleDrawer(true, {
      functionId: unref(selectedKeys)[0],
      roleId: unref(roleId),
      roleCode: unref(roleCode),
      btdPermissionId: btdPermissionId.value || '',
      selectedData: unref(selectedMenuData),
    });
  }

  /**
   * 数据重置
   */
  function reset() {
    treeData.value = [];
    allTreeKeys.value = [];
    checkedKeys.value = [];
    defaultCheckedKeys.value = [];
    selectedKeys.value = [];
    roleId.value = '';
  }
  /**
   * 获取tree实例
   */
  function getTree() {
    const tree = unref(treeRef);
    if (!tree) {
      throw new Error('tree is null!');
    }
    return tree;
  }
  /**
   * 提交
   */
  async function handleSubmit(exit) {
    let params = {
      roleId: unref(roleId),
      permissionIds: unref((getTree() as any)?.getCheckedKeys())?.join(','),
      lastpermissionIds: unref(defaultCheckedKeys).join(','),
    };
    if (loading.value === false) {
      await doSave(params);
      doSaveBtdPermission();
    } else {
      console.log('请等待上次执行完毕!');
    }
    if (exit) {
      // 如果关闭
      closeDrawer();
    } else {
      // 没有关闭需要重新获取选中数据
      const permResult = await queryRolePermission({ roleId: unref(roleId) });
      defaultCheckedKeys.value = permResult;
    }
  }

  // VUE角色授权重复保存 #352
  async function doSave(params) {
    loading.value = true;
    try {
      await saveRolePermission(params);
    } catch (e) {
      loading.value = false;
    }
    setTimeout(() => {
      loading.value = false;
    }, 500);
  }

  // 保存轻应用菜单权限
  function doSaveBtdPermission() {
    // 获取百特搭权限iframe
    const btdPermissionIframe = document.getElementById('btd-permission-iframe') as HTMLIFrameElement;
    if (btdPermissionIframe) {
      const message = {
        type: 'OnSave',
        data: JSON.stringify({
          treeData: treeData.value,
          checked: checkedKeys.value,
          formFields: {
            formKey: 'menuResourceId',
            appId: 'menuResourceAppId',
          }, //这固定写死
        }),
      };
      btdPermissionIframe?.contentWindow?.postMessage(message, '*');
    }
  }

  /**
   * 树菜单选择
   * @param key
   */
  function treeMenuClick({ key }) {
    if (key === 'checkAll') {
      checkedKeys.value = allTreeKeys.value;
    } else if (key === 'cancelCheck') {
      checkedKeys.value = [];
    } else if (key === 'openAll') {
      expandedKeys.value = allTreeKeys.value;
      saveLocalOperation('expand', 'openAll');
    } else if (key === 'closeAll') {
      expandedKeys.value = [];
      saveLocalOperation('expand', 'closeAll');
    } else if (key === 'relation') {
      checkStrictly.value = false;
      saveLocalOperation('level', 'relation');
    } else {
      checkStrictly.value = true;
      saveLocalOperation('level', 'standAlone');
    }
  }
  /**
   * 2024-05-31
   * liaozhiyang
   * 【TV360X-590】角色授权弹窗操作缓存
   * */
  const saveLocalOperation = (key, value) => {
    const localData = localStorage.getItem(ROLE_AUTH_CONFIG_KEY);
    const obj = localData ? JSON.parse(localData) : {};
    obj[key] = value;
    localStorage.setItem(ROLE_AUTH_CONFIG_KEY, JSON.stringify(obj));
  };
</script>

<style lang="less" scoped>
  /** 固定操作按钮 */
  .cyj-basic-tree {
    position: absolute;
    width: 618px;
  }
  .line {
    height: 1px;
    width: 100%;
    border-bottom: 1px solid #f0f0f0;
  }
  .more-icon {
    font-size: 20px !important;
    color: black;
    display: inline-flex;
    float: right;
    margin-right: 2px;
    cursor: pointer;
  }
  :deep(.cyj-tree-header) {
    border-bottom: none;
  }
</style>
<style lang="less">
  .system-role-permission-drawer{
    &.cyj-basic-drawer .ant-drawer-body .scrollbar__wrap{
      padding: 6px 8px !important;
    }
  }
</style>